using System;
using System.Web;

namespace ComplianceFilter
{
	/// <summary>
	/// HttpModule to filter various .NET standards compliance screwups
	/// </summary>
	public class ComplianceFilter : IHttpModule
	{
		public void Init (HttpApplication app)
		{
			app.ReleaseRequestState += new EventHandler(InstallResponseFilter);
		}

		private void InstallResponseFilter(object sender, EventArgs e) 
		{
			HttpResponse response = HttpContext.Current.Response;

			if(response.ContentType == "text/html"){
				//we are not in umbraco admin so clean away	

                // Should this URL not use the compliance filter.
                bool notInReservedUrl = true;
                foreach (String reservedUrl in Configuration.UmbracoReservedUrls)
                {
                    if (HttpContext.Current.Request.Url.AbsoluteUri.IndexOf(reservedUrl) > -1)
                    {
                        notInReservedUrl = false;
                        break;
                    }
                }

                if (notInReservedUrl)
				{	
					response.Filter = new PageFilter (response.Filter);
				}					
			}			
			
		}


		public void Dispose()
		{

		}
	}
}
